package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.constant.Sysconst;
import cn.chinaflame.pagodas.dto.AgentLoginDto;
import cn.chinaflame.pagodas.dto.PaAgentDto;
import cn.chinaflame.pagodas.entity.PaAgent;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaAgentRepository;
import cn.chinaflame.pagodas.req.LoginReq;
import cn.chinaflame.pagodas.req.PaAgentQryReq;
import cn.chinaflame.pagodas.req.PaAgentReq;
import cn.chinaflame.pagodas.service.PaAgentService;
import cn.chinaflame.pagodas.service.YwAreaService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 代理相关接口
 *
 * @author boming
 * @date 2018年6月21日 下午1:58:07
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/agent/")
public class OpenPaAgentAPI {
    private static final Logger log = LoggerFactory.getLogger(OpenPaAgentAPI.class);
    @Autowired
    private PaAgentService ywAgentService;
    @Autowired
    private PaAgentRepository ywAgentRepository;
    @Autowired
    private YwAreaService ywAreaService;

    /**
     * 代理登录接口
     *
     * @param req
     * @return
     */
    @PostMapping(value = "login")
    @ApiDoc(AgentLoginDto.class)
    public Object login(@RequestBody LoginReq req, HttpServletResponse response) {
        log.info("佛龛代理登录req:{}", req);
        req.check();
        AgentLoginDto dto = ywAgentService.login(req.getUsername(), req.getPassword(), response);
        return Result.create(dto);
    }
    
    /**
     * 代理登出接口
     * @return
     */
    @PostMapping(value = "loginout")
    @ApiDoc(AgentLoginDto.class)
    public Object loginout(){
    	return Result.create("成功");
    }
    /**
     * 代理修改密码接口
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @GetMapping(value = "modifyPwd")
    public Object modify(String oldPwd, String newPwd, String id){
    	log.info("修改密码oldPwd:{},newPwd:{},id:{}", oldPwd, newPwd, id);
    	ywAgentService.modifyPwd(oldPwd, newPwd, id);
    	return Result.create("修改成功");
    }

    /**
     * 添加新代理人
     *
     * @param req
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(value = PaAgentReq.class, result = PaAgentDto.class)
    public Object add(@RequestBody PaAgentReq req) {
        log.info("代理添加req:{}", req);
        PaAgentDto dto = ywAgentService.add(req);
        return Result.create(dto);
    }

    /**
     * 查询代理列表req
     *
     * @param req
     * @return
     */
    @PostMapping(value = "query/list")
    @ApiDoc(value = PaAgent.class, result = PaAgentReq.class)
    public Object queryList(@RequestBody PaAgentReq req) {
        log.info("查询代理列表req:{}", req);
        return ywAgentService.fingAll(req.getPageIndex(), req.getPageSize(),
                req.getProvinceCode(), req.getCityCode(), req.getAreaCode(), req.getProjectId(), req.getId());
    }

    /**
     * 查询下级代理列表
     *
     * @param parentId 代理id
     * @return
     */
    @GetMapping(value = "query/list/searchByParentId")
    @ApiDoc(PaAgentDto.class)
    public Object queryByParentId(String parentId) {
        log.info("查询下级代理列表parentId:{}", parentId);
        return ywAgentService.queryByParent(parentId);
    }

    /**
     * 查询下级代理列表
     *
     * @param parentId     父代理id
     * @param provinceCode 省编码
     * @param cityCode     市编码
     * @param areaCode     区编码
     * @return
     */
    @GetMapping(value = "query/list/searchByParentIdAndArea")
    @ApiDoc(PaAgentDto.class)
    public Object queryByParentIdAndArea(String parentId, String provinceCode, String cityCode, String areaCode) {
        log.info("查询下级代理列表parentId:{},provinceCode:{},cityCode:{},areaCode:{}",
                parentId, provinceCode, cityCode, areaCode);
        return ywAgentService.queryByParentIdAndArea(parentId, provinceCode, cityCode, areaCode);
    }

    /**
     * 查询代理结构树
     *
     * @param req
     * @return
     */
    @PostMapping(value = "query/list/tree")
    @ApiDoc(value = PaAgentQryReq.class, result = PaAgentDto.class)
    public Object queryById(@RequestBody PaAgentQryReq req) {
        log.info("查询代理结构树列表req:{}", req);
        return ywAgentService.queryByTree(req);
    }

    /**
     * 查询下级代理结构树
     *
     * @param id
     * @return
     */
    @GetMapping(value = "query/list/searchById")
    @ApiDoc(PaAgentDto.class)
    public Object queryById(String id) {
        log.info("查询下级代理结构树id:{}", id);
        return ywAgentService.queryById(id);
    }

    /**
     * 查询下级代理ID
     *
     * @param id 代理id
     * @return
     */
    @GetMapping(value = "query/list/searchAtyId")
    @ApiDoc(Result.class)
    public Object queryAryId(String id) {
        log.info("查询下级代理IDid:{}", id);
        return ywAgentService.queryAryId(id);
    }

    /**
     * 查询代理
     *
     * @param projectId 项目id
     * @return
     */
    @GetMapping(value = "query/list/searchByProjectId")
    @ApiDoc(PaAgentDto.class)
    public Object queryByProjectId(String projectId) {
        log.info("查询代理列表projectId:{}", projectId);
        return ywAgentService.queryByProjectId(projectId);
    }

    /**
     * 根据代理查询区域
     *
     * @param agentid 代理id
     * @return
     */
    @GetMapping("query/agents")
    @ApiDoc(Result.class)
    public Object querybyagents(String agentid) {
        log.info("根据代理查询区域agentid:{}", agentid);
        ParamPreconditions.checkNotEmpty(agentid, CodeEnum.FAIL.getCode(), "代理信息不能为空");
        String rs = ywAreaService.queryByAgents(agentid);
        log.info("rs数据:{}", rs);
        return Result.create(rs);
    }

    /**
     * 单个代理查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "query/{id}")
    @ApiDoc(PaAgentDto.class)
    public Object query(@PathVariable String id) {
        log.info("查询代理详情id:{}", id);
        return ywAgentService.query(id);
    }

    /**
     * 更新代理人
     *
     * @param dto
     * @return
     */
    @PutMapping(value = "update")
    @ApiDoc(PaAgentDto.class)
    public Object updateAgent(@RequestBody PaAgentDto dto) {

        return Result.create(ywAgentService.updateAgent(dto));
    }


    /**
     * 删除代理人
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    @ApiDoc(Result.class)
    public Object deleteAgent(@PathVariable("id") String id) {
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "代理id不能为空");
        PaAgent agent = ywAgentRepository.findOne(id);
        ParamPreconditions.checkArgument(agent != null && !Sysconst.TREE_ROOT_PARENT.equals(agent.getParentId()),
                CodeEnum.FAIL.getCode(), "炎武总代理不能删除");
        ywAgentRepository.delete(id);
        return Result.create(true);
    }

    @GetMapping("test")
    public Object test() {
        ywAgentService.test();
        return null;
    }

}









